/*
	new 用来给类型分配一片空间，得到的是一个地址
*/

package main

import (
	"log"
)

type people struct {
	id  int
	age int
}

/*
	p1 : 值传递
	p2 : 引用传递：&
	p3 : 引用传递：切片
	p4 : 引用传递：new
*/
func getStrPeople() (p1 people, p2 *people, p3 []people, p4 *people) {
	p1 = people{id: 2}

	p2 = &people{2, 20}

	p3 = make([]people, 1)
	p3[0].id = 3
	p3[0].age = 30

	// new
	p4 = new(people)
	p4.id = 4
	p4.age = 40

	return p1, p2, p3, p4
}

func modifyStrPeople(p1 people, p2 *people, p3 []people, p4 *people) {
	p1.id = 100
	p1.age = 1

	p2.id = 200
	p2.age = 2

	p3[0].id = 300
	p3[0].age = 3

	p4.id = 400
	p4.age = 4

	println("更改时：")
	log.Printf("%p : %v\n", &p1, p1)
	log.Printf("%p : %v\n", p2, p2)
	log.Printf("%p : %v\n", p3, p3)
	log.Printf("%p : %v\n", p4, p4)
}

func main() {

	log.SetFlags(log.Lshortfile)

	p1, p2, p3, p4 := getStrPeople()

	log.Printf("%p : %v\n", &p1, p1)
	log.Printf("%p : %v\n", p2, p2)
	log.Printf("%p : %v\n", p3, p3)
	log.Printf("%p : %v\n", p4, p4)

	modifyStrPeople(p1, p2, p3, p4)

	log.Println("更改后：")

	log.Printf("%p : %v\n", &p1, p1)
	log.Printf("%p : %v\n", p2, p2)
	log.Printf("%p : %v\n", p3, p3)
	log.Printf("%p : %v\n", p4, p4)
}
